package com.example.demo.Controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SecurityController {

    @RequestMapping({"/","/index"})
    public String Hello(Model model){
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        if(authentication.getName() != "anonymousUser"){
            model.addAttribute("authentication",authentication);
            return "index";
        }
        else{
            return "redirect:/login";
        }
    }

    @RequestMapping("/common_error")
    @ResponseBody
    public String error()
    {
        return "Oops!An error occurs";
    }

    @RequestMapping("/show_charts")
    public String show_charts()
    {
        return "common/charts";
    }
}